//
//  CZEmoticonTools.swift
//  Blog
//
//  Created by whitcast on 16/7/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 一页显示20个表情数据
let numbersOfPage = 20
// 读取表情工具专用类
class CZEmoticonTools: NSObject {
    // 定义单例全局访问点
    static let shareTools = CZEmoticonTools()
    
    // 构造函数私有化
    private override init() {
        super.init()
    }
    
    // 创建bundle对象
    private lazy var emoticonBundle: NSBundle = {
        // 获取路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType:"bundle")!
        // 创建bundle对象
        let bundle = NSBundle(path: path)!
        
        return bundle
    }()
    
    // 读取默认表情数据
    private lazy var defaultEmoticonArray: [CZEmoticon] = {
        
        return self.loadEmoticonsWithPath("default/info.plist")
        
    }()
    //  读取emoji表情数据
    private lazy var emojiEmoticonArray: [CZEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    
    //  读取浪小花表情数据
    private lazy var lxhEmoticonArray: [CZEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    // 准备表情视图需要的数据
    lazy var allEmoticonArray: [[[CZEmoticon]]] = {
        
        return [
            
            self.pagesWithEmotions(self.defaultEmoticonArray),
            self.pagesWithEmotions(self.emojiEmoticonArray),
            self.pagesWithEmotions(self.lxhEmoticonArray)
            
        ]
    }()

    // 读取表情通用方法，根据子路径
    private func loadEmoticonsWithPath(subPath: String) -> [CZEmoticon]{
        // 获取bundle对象
        let bundle: NSBundle = self.emoticonBundle
        // 获取info.plist
        let path = bundle.pathForResource(subPath, ofType: nil)!
        // 获取plist文件里面内容
        let emoticonDicArray = NSArray(contentsOfFile: path)!
        
        var tempArray = [CZEmoticon]()
        // 遍历数组模型
        for emoticonDic in emoticonDicArray {
            let emoticon = CZEmoticon(dic: emoticonDic as! [String : AnyObject])
            //  如果是图片需要处理路径
            if emoticon.type == "0"{
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emoticon.path = lastPath + "/" + emoticon.png!
            }
            tempArray.append(emoticon)
        }
        return tempArray

    }
    //  把表情数据拆分成一个二维数组
    private func pagesWithEmotions(emoticon: [CZEmoticon]) -> [[CZEmoticon]] {
        
        //  根据图片的个数计算页数
        let pages = (emoticon.count - 1) / numbersOfPage + 1
        
        var tempArray = [[CZEmoticon]]()
        for i in 0..<pages {
            // 截取的索引
            let loc = i * numbersOfPage
            // 截取的个数
            var len = numbersOfPage
            //  如果截取长度不够则取到剩余个数
            if loc + len > emoticon.count {
                len = emoticon.count - loc
            }
            
            let subArray = (emoticon as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [CZEmoticon]
            tempArray.append(subArray)
        }
        
        return tempArray
        
    }

 
}
